package com.zzzgroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.zzzgroup.andtomato.R;

public class SettingsMenu1 extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingsmenu);

		// getWorkTimeEditText().setText("30");
		// getRestTimeEditText().setText("15");
		InitWorkTimeSpinner();
		InitRestTimeSpinner();
		getOKButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// long workTime =
				// Long.parseLong(getWorkTimeEditText().getText().toString());
				// long restTime =
				// Long.parseLong(getRestTimeEditText().getText().toString());

				// AppController.Instance().setTime(workTime, restTime);
				Long workTime = Long.parseLong(getWorkTimeSpinner()
						.getSelectedItem().toString()) * 60;
				Long restTime = Long.parseLong(getRestTimeSpinner()
						.getSelectedItem().toString()) * 60;
				AppController.Instance().setTime(workTime, restTime);
				CloseMenu();

			}
		});

		getCancelButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CloseMenu();

			}
		});

	}

	private void CloseMenu() {
		this.finish();
	}

	private void InitWorkTimeSpinner() {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.TimeValues, R.layout.textview);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getWorkTimeSpinner().setAdapter(adapter);

		int k = adapter.getPosition(Integer.toString(AppController.Instance()
				.getTime().getWorkTimeInMinutes()));
		if (k > 0) {
			getWorkTimeSpinner().setSelection(k);
		}
	}

	private void InitRestTimeSpinner() {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.TimeValues, R.layout.textview);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getRestTimeSpinner().setAdapter(adapter);

		int k = adapter.getPosition(Integer.toString(AppController.Instance()
				.getTime().getRestTimeInMinutes()));
		if (k > 0) {
			getRestTimeSpinner().setSelection(k);
		}
	}

	public Button getOKButton() {
		return (Button) findViewById(R.id.button1);
	}

	public Button getCancelButton() {
		return (Button) findViewById(R.id.button2);
	}

	private Spinner _workTimeSpinner = null;

	public Spinner getWorkTimeSpinner() {
		if (_workTimeSpinner == null)
			_workTimeSpinner = (Spinner) this
					.findViewById(R.id.workTimeSetting);
		return _workTimeSpinner;
	}

	private Spinner _restTimeSpinner = null;

	public Spinner getRestTimeSpinner() {
		if (_restTimeSpinner == null)
			_restTimeSpinner = (Spinner) this
					.findViewById(R.id.restTimeSetting);
		return _restTimeSpinner;
	}

}
